#include "..\Inc\Includes.h"
/***************************************************************************************
	Model		: Stdop
	Description	:  
	Author		: Along
	Create Time	: 2022-02-11
	Version ID  : 0.1
****************************************************************************************/
s16 Stdop_ParaAdd(s16 para,s16 min,s16 max,bool cycle,s16 unit)
{
    s16 buf = para;

	if(max == buf)
	{
	    if(false == cycle)
	        return max;
        else
			return (min+unit-1);
	}
    if((buf+unit)>max)
        return max;
    else
        return (buf+unit);
}

s16 Stdop_ParaSub(s16 para,s16 min,s16 max,bool cycle,s16 unit)
{
    s16 buf = para;
    
	if(min == buf)    
	{
        if(false == cycle)
            return min;
        else
            return (max-unit+1);
    }
    
	if((buf - unit) < min)
		return min;
    else
        return (buf - unit);
}

void Stdop_HexToAscii(u8 para,u8* high,u8* low)
{
	U8 buf;

    if((NULL == high)||(NULL == low))
        return;    

	buf = para>>4; 

    if(buf < 10)		
  	    *high = buf + '0';
	else
		*high = buf - 10 + 'A';

    buf = para & 0x0f;

    if(buf < 10)		
  	    *low = buf + '0';
	else
		*low = buf - 10 + 'A';
}

void Stdop_AsciiToHex(u8 high,u8 low,u8* para)
{
    if(NULL == para)
        return;

    if(high>='a' && high<='f')
  	    high=high-'a'+10;
    else if(high>='A' && high<='F')
  	    high=high-'A'+10;
    else if(high>='0' && high<='9')
  	    high=high-'0';	
	else 
		high=0;
    
    if(low>='a' && low<='f')
  	    low=low-'a'+10;
    else if(low>='A' && low<='F')
  	    low=low-'A'+10;
    else if(low>='0' && low<='9')
  	    low=low-'0';	
	else 
		low=0;

	*para = high<<4;
	*para += (low&0x0f);
}

bool Stdop_QueueHexToAscii(u8 *source,u8 size,u8 *target)
{
    u8 i;

    if((NULL == source)||(NULL == target)||(0==size))
        return false;

    for(i=0;i<size;i++)
    {       
        Stdop_HexToAscii(*source,target+i*2,target+i*2+1);
        source++;
    }
    return true;
}


bool Stdop_QueueAsciiToHex(u8 *source,u8 size,u8 *target)
{
    u8 i, high,low;

    if((NULL == source)||(NULL == target))
        return false;

    if((0 != (size%2)) || (size < 2))
        return false;

    for(i=0;i<(size/2);i++)
    {
        high = *source;
        source++;
        low = *source;
        source++;
        Stdop_AsciiToHex(high, low, &target[i]);        
    }
    return true;
}

void Stdop_MemoryCopy(void *source, void *target,u16 size)
{
	  u16 i;
    u8 *index1,*index2;

    if((NULL == source)||(NULL == target))
        return;

    index1 = (u8*)source;
    index2 = (u8*)target;
    
	for(i=0;i<size;i++)
	{
	    *index2 = *index1;
        index1++;
        index2++;	
	}
}

void Stdop_MemoryClear(void *source,u16 size)
{
	u16 i;
	u8 *index;

    if(NULL == source)
        return;	

    index = (u8*)source;

    for(i=0;i<size;i++)
	{
		*index = 0;	
        index++;
    }
}
bool Stdop_MemoryCompare(void *dat1,void *dat2,u16 size)
{
	u16 i;
    u8 *index1,*index2;
    
    if((NULL == dat1)||(NULL == dat2))
        return false;
    
    index1 = (u8*)dat1;
    index2 = (u8*)dat2;

    for(i=0;i<size;i++)
    {
        if((*index1) != (*index2))
            return false;
        
        index1++;
        index2++;        
    }
    return true;
}

void Stdop_MemorySet(void *dat,u8 para,u16 size)
{
    u16 i;
    u8 *index;

    if(NULL == dat)
        return;

    index = (u8*)dat;

    for(i=0;i<size;i++)
    {
        *index = para;
        index++;
    }
}



